/**
 * javascript Comment Templates(jCT) 2(第二版)
 *
 * Copyright (c) 2008 维拉数码 (http://uf264.com)
 * licensed under the MIT license.
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Author achun (achun.shx at gmail.com)
 * Create Date: 2008-4-2
 * Last Date: 2008-4-7
 * Revision:2.00
 * Note:jCT 2 not compatible jCT 1
 *
 * 注意:jCT 2 是重新设计的,和 jCT 1 的语法不兼容.jCT 1 已经放弃.
 */
var jCT = window.jCT = function(txt,opt){
	opt = opt || 'parse';
	if (opt!='parse' && opt!='clean') return false;
	if (!txt) txt = document.body.innerHTML;
	if (opt=='clean'){
		var tmp,jct=[],tag,p2=[0,0,0,txt.length,0];
		return clean();
	}
	if (window==this) return new jCT(txt,opt);
	this.CTA=[];
	var self=this,tmp,jct=[],sta,tag,p1=[0,0,0,txt.length,0],p2=[0,0,0,txt.length,0],p3=[];
	parse();
	jct.push('__CTV__.push(this.CTA['+this.CTA.length+']);');
	this.Error=false;
	this.CTA.push(txt.slice(p2[0]));
	tag='function(__CTD__){if (!__CTD__) __CTD__={};\nvar __CTV__=[];\nwith(__CTD__){\n'+jct.join('\n')+'}\nreturn __CTV__.join("");}';
	try{
		this.Load=eval(tag);
	}catch (ex){
		this.Error={message:(ex.lineNumber || ex.number) +':'+ ex.message,src:tag};
		this.Load=function(){return 'jCT Parse Error on line '+ this.Error.message;};
	}
	return this;
	function clean(){
		while (slice('<!--','-->',p2[0],2)) {
			jct.push(txt.slice(p2[1],p2[2]));
			tag=txt.slice(p2[3],p2[4]).indexOf(' ');
			tag=(tag<0)? p2[4] :p2[3]+ tag;
			tmp=txt.slice(p2[3],tag);
			switch (tmp) {
				case '!!':
					if (slice('','<!--!!-->',p2[0],2)) jct.push(txt.slice(p2[3],p2[4]));
					continue;
				case '!$':slice('','<!--!-->',p2[0],2);continue;
			}
			jct.push(txt.slice(p2[2],p2[0]));
		}
		jct.push(txt.slice(p2[0]));
		return jct.join('');
	}
	function parse(){
		while (slice('<!--','-->',p2[0],2)){//其他流控
			p1[0]=p2[1];
			while (slice('+-','-+',p1[0],1)){//取值表达式
				jct.push('__CTV__.push(this.CTA['+self.CTA.length+']);');
				self.CTA.push(txt.slice(p1[1],p1[2]));
				jct.push('__CTV__.push('+txt.slice(p1[3],p1[4])+');');
			}
			jct.push('__CTV__.push(this.CTA['+self.CTA.length+']);');
			self.CTA.push(txt.slice(p1[0],p2[2]));
			tag=txt.slice(p2[3],p2[4]).indexOf(' ');
			tag=(tag<0)? p2[4] :p2[3]+ tag;
			sta=false;
			tmp=txt.slice(p2[3],tag);
			if ('!$ end else elseif if for while case continue break switch default do enddo var function run with !! !# !@'.indexOf(tmp)>=0) {
				sta=txt.slice(tag,p2[4]);
				switch (tmp) {
					case '!$':slice('','<!--!-->',p2[0],2);continue;
					case 'end':      sta='}';break;
					case 'else':     sta="}else{"+sta;break;
					case 'elseif':   sta="}else if("+sta+"){";break;
					case 'if':       sta="if("+sta+"){";break;
					case 'for':      sta="for("+sta+"){";break;
					case 'while':    sta="while("+sta+"){";break;
					case 'case':     sta='case '+sta + ':';break;
					case 'continue': sta='continue;';break;
					case 'break':    sta='break;';break;
					case 'switch':   sta="switch("+sta+"){";break;
					case 'default':  sta='default:';break;
					case 'do':       sta="do("+sta+"){";break;
					case 'enddo':    sta="}while("+sta+");";break;
					case 'var':      sta='var ' + sta ;break;
					case 'function': sta='function ' + sta;break;
					case 'run':      break;
					case 'with':     sta="with("+sta+"){";break;
					case '!!':
						if (slice('','<!--!!-->',p2[0],2)){
							jct.push('__CTV__.push(this.CTA['+self.CTA.length+']);');
							self.CTA.push(txt.slice(p2[3],p2[4]));
						}
						continue;
					case '!#':
						p3[0]=p2[0];p3[1]=p2[1];p3[2]=p2[2];p3[3]=p2[3];p3[4]=p2[4];
						if (slice('','<!--!-->',p2[0],2)){
							self[txt.slice(tag+1,p3[4])]=jCT(txt.slice(p2[3],p2[4]));
						}
						continue;
					case '!@':
						p3[0]=p2[0];p3[1]=p2[1];p3[2]=p2[2];p3[3]=p2[3];p3[4]=p2[4];
						if (slice('','<!--!-->',p2[0],2)){
							self[txt.slice(tag+1,p3[4])]=jCT(txt.slice(p2[3],p2[4]));
						}
						p2[0]=p3[0];p2[1]=p3[1];p2[2]=p3[2];p2[3]=p3[3];p2[4]=p3[4];
						continue;
					default:sta=false;
				}
				if (sta) jct.push(sta);
			}
		}
		while (slice('+-','-+',p2[0],2)){//取值表达式
			jct.push('__CTV__.push(this.CTA['+self.CTA.length+']);');
			self.CTA.push(txt.slice(p2[1],p2[2]));
			jct.push('__CTV__.push('+txt.slice(p2[3],p2[4])+');');
		}
	}
	function slice(begin,end,b1,p) {
		if ((p==1 && b1+begin.length>p2[3]))return false;
		var e1,b2,e2;
		e1=txt.indexOf(begin,b1);
		b2=e1+begin.length;
		if(e1<0 || (p==1 && b2>p2[3])) return false;
		e2=txt.indexOf(end,b2);
		if(e2<0  || (p==1 && e2+end.length>p2[3])) return false;
		if (p==1) p1=[e2+end.length,b1,e1,b2,e2];
		else p2=[e2+end.length,b1,e1,b2,e2];
		return true;
	}
}